package org.rbac.action;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.rbac.content.RbacApplicationContent;
import org.rbac.serv.ActionServices;
import org.rbac.vo.Action;
import org.rbac.vo.ActionReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
 * 资源管理
 * 
 * <pre>
 * 项目名称：rbac
 * 类名称：ActionAction
 * 类描述：资源管理
 * 创建人：唐家林(tjl)
 * 创建时间：2014年9月28日　上午11:53:19
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Controller
@RequestMapping(value = "/action")
public class ActionAction {

	@Autowired
	private ActionServices actionServ;
	@Resource(name="RbacApplicationContent")
    private RbacApplicationContent content;

	/**
	 * 修改
	 * 
	 * @param a
	 *            修改的对象数据
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public String modifyAction(Action a) {
		ActionReturn ar = new ActionReturn();
		try {
			actionServ.moidifyAction(a);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 启用
	 * 
	 * @param ids
	 *            启用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/enable")
	@ResponseBody
	public String enableAction(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 1;
			actionServ.moidifyActionStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 禁用
	 * 
	 * @param ids
	 *            禁用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/disenable")
	@ResponseBody
	public String disenableAction(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 2;
			actionServ.moidifyActionStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 *            删除的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String deleteAction(@RequestParam("ids[]") Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 0;
			actionServ.moidifyActionStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 进入列表
	 * 
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list")
	public ModelAndView actionList() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/rbac/action");
		return mav;
	}
	/**
     * 菜单列表
     * 
     * @return 返回视图
     */
    @RequestMapping(value = "/menulist")
    public ModelAndView menulist() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/rbac/menu");
        return mav;
    }
	/**
	 * 得到菜单图标
	 * @return
	 * @throws JSONException 
	 */
	@ResponseBody
	@RequestMapping(value="/menuIcons")//, produces = { "application/json;charset=UTF-8" }
	public String getMenuIcons(HttpServletRequest req) throws JSONException{
	    String folder= req.getServletContext().getRealPath("res/img/menu");
        File f = new File(folder);
        JSONArray js = new JSONArray();
        JSONObject json =null;
        if (f.exists()) {
            File[] fs =f.listFiles();
            for (File file : fs) {
                json= new JSONObject();
                json.put("id", file.getName());
                json.put("text", file.getName());
                js.put(json);
            }
        } 
        js.put(new JSONObject("{id:'icon-add',text:'icon-add'}"));
        js.put(new JSONObject("{id:'icon-edit',text:'icon-edit'}"));
        js.put(new JSONObject("{id:'icon-ok',text:'icon-ok'}"));
        js.put(new JSONObject("{id:'icon-cancel',text:'icon-cancel'}"));
        js.put(new JSONObject("{id:'icon-no',text:'icon-no'}"));
        js.put(new JSONObject("{id:'icon-useradd',text:'icon-useradd'}"));
        js.put(new JSONObject("{id:'icon-userdelete',text:'icon-userdelete'}"));
        js.put(new JSONObject("{id:'icon-useredit',text:'icon-useredit'}"));
        js.put(new JSONObject("{id:'icon-vcardedit',text:'icon-vcardedit'}"));
        
        return js.toString();
	    
	}
	
	@ResponseBody
    @RequestMapping(value="/updateMapping")
    public String updateAppMapping(HttpServletRequest req) throws Exception{
	    String flag="ng";
       try {
           content.setApplicationMapping();
           req.getServletContext().setAttribute("skin", "gray");
           flag="ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
	
}
